#ifndef TCACHE_H
#define TCACHE_H

#include "crossbit/common.h"
#include "crossbit/MemoryManager.h"
#include "crossbit/TBlock.h"
#include "crossbit/VBlock.h"
#include "crossbit/HashMap.h"

#define appnum 128		//Assign a number divided by 32, show how many TBlock is allocated at one time
#define avginblk 20		//Assign a number divided by 4, assess how many instructions in one block in average

namespace crossbit {

	class TCache {
		public:

			TCache( XTUint32 size, MemoryManager *mm );
			virtual ~TCache();
			// Begin create a new TBlock
			// Return pointer to TBlock, NULL if fails.
			virtual TBlock *requestTBlock();
			// End crreate the cache item "spc,tblock,vblock"
			virtual void submitTBlock(XTMemAddr spc, TBlock *tblock, VBlock *vblock);
			// Lookup the TBlock from TCache with *Source PC*
			virtual TBlock *lookupTBlock(XTMemAddr spc);
			virtual VBlock *lookupVBlock(XTMemAddr spc);
			void eraseTBlock(TBlock *tblock);
			virtual void replacePolicy() {}
		protected:
			XTUint32		tc_pool_id;
			XTUint32		tc_pool_state_id;

			XTUint32 		tc_size;
			MemoryManager		*tc_mm;	

			HashMap			tc_map;
			XTMemAddr		tc_tcode;
			XTMemAddr		tc_cur_tcode;	
			
			TBlock                  *tb_top;		//Maintain the top point to the TBlock array
                        XTUint32                tb_total;		//Maintain the largest number of TBlocks

			XTUint32		*tb_state;		//Maintain the status of these TBlocks
			XTUint32		*tb_state_grp;		//Maintain the state group value

			XTUint32		tb_cur_init;		//Maintain how many tb_block initialized
			XTUint32		tb_cur_avail;		//Show how many TBlock objects available now
		};
}

#endif
